<?php
namespace app\common\library;
use app\common\model\Expense;
use app\common\model\Trade;
use app\common\model\User;
use think\facade\Log;
class OrderExpense{
    /**
     * 消费预结算（Events使用）
     **/
    public static function prepaied($expense_id,$total,$amount){
        Log::info('prepaied:'.$expense_id.' rmb: '.$total.$amount);
        $expense  = Expense::find($expense_id);
        if(empty($expnse)){
            return false;
        }
        if($expense['finished'] == 1){
            return true;
        }
        $total = $amount > 50 ? $total+1 : $total;
        $prepay_amount = 0;
        if($expense['type'] == 'gift'){
            if($total>10){
                $prepay_amount = $total;
            }else{
                $prepay_amount = 10;
            }
            
        }else{
            $user = User::find($expense->user_id);
            if($total - $user->balance_zs > 6){
                $settlement = $total;
            }else{
                if($user->balance_zs >=4){
                    $prepay_amount = 10;
                }else{
                    $prepay_amount = 6+$user->balance_zs;
                }
                
            }
        }
        return $prepay_amount;
    } 
    /**
     * 消费结算
     */
    public static function settlement($expense_id,$total,$amount){
        Log::info('settlement:'.$expense_id.' rmb: '.$total.$amount);
        $expense  = Expense::find($expense_id);
        
        if(empty($expnse)){
            return false;
        }
        
        if($expense['finished'] == 1){
            return true;
        }
        $total = $amount > 50 ? $total+1 : $total;
        $user = User::find($expense->user_id);
        $settlement = 0;
        
        if($expense['type'] == 'gift'){
            if($total>10){
                $settlement = $total;
            }else{
                $settlement = 10;
            }
            if($user['balance_zs'] >= $settlement){
                $expense->total = $settlement;
                $expense->finished = 1;
                $expense->prepaied =0;
                $expense->save();
                Trade::create([
                    'user_id'=> $expense->user_id,
                    'trade_type'=>'expense',
                    'order_id'=> $expense->id,
                    'type'=>1,
                    'balance'=> $settlement,
                    'remark'=>'洗车消费赠送余额'.$settlement.'元',
                ]);
            }else{
                if($user->balance_zs >0){
                    $expense->total = $user->balance_zs;
                    $expense->finished = 1;
                    $expense->prepaied =0;
                    $expense->save();
                    Trade::create([
                        'user_id'=> $expense->user_id,
                        'trade_type'=>'expense',
                        'order_id'=> $expense->id,
                        'type'=>1,
                        'balance'=> $user->balance_zs,
                        'remark'=>'洗车消费赠送余额'.$user->balance_zs.'元',
                    ]);
                    $more = $settlement-$user['balance_zs'];
                    $more_exp = Expense::create([
                        'user_id'=>$expense->user_id,
                        "out_trade_no"=>date('ymd', time()) . mt_rand(1000, 9999) . substr(time(), '3', '8'),
                        "device_id"=>$expense['id'],
                        "engine_id"=>$expense['engine_id'],
                        "type"=>'balance',
                        "total"=>$more,
                        "prepaied"=>'0'
                    ]);
                    Trade::create([
                        'user_id'=> $more_exp->user_id,
                        'trade_type'=>'expense',
                        'order_id'=> $more_exp->id,
                        'type'=>0,
                        'balance'=> $more,
                        'remark'=>'洗车消费余额'.$more.'元',
                    ]);
                }else{
                    $expense->total = $settlement;
                    $expense->type = 'balance';
                    $expense->finished = 1;
                    $expense->prepaied =0;
                    $expense->save();
                    Trade::create([
                        'user_id'=> $expense->user_id,
                        'trade_type'=>'expense',
                        'order_id'=> $expense->id,
                        'type'=>0,
                        'balance'=> $settlement,
                        'remark'=>'洗车消费余额'.$settlement.'元',
                    ]);
                }
            }
        }else{
            $settlement = $total;
            if($total >= 10){
                if($user['balance_zs'] >=$total){
                    $expense->total = $settlement;
                    $expense->type = 'gift';
                    $expense->finished = 1;
                    $expense->prepaied =0;
                    $expense->save();
                    Trade::create([
                        'user_id'=> $expense->user_id,
                        'trade_type'=>'expense',
                        'order_id'=> $expense->id,
                        'type'=>1,
                        'balance'=> $settlement,
                        'remark'=>'洗车消费赠送余额'.$settlement.'元',
                    ]);
                }else{
                    if($user['balance_zs'] > 0){
                        if($total-$user['balance_zs'] >= 6){
                            $more = $total-$user['balance_zs'];
                        }else{
                            $more = 6;
                        }
                        $expense->total = $user['balance_zs'];
                        $expense->type = 'gift';
                        $expense->finished = 1;
                        $expense->prepaied =0;
                        $expense->save();
                        Trade::create([
                            'user_id'=> $expense->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $expense->id,
                            'type'=>1,
                            'balance'=> $user['balance_zs'],
                            'remark'=>'洗车消费赠送余额'.$user['balance_zs'].'元',
                        ]);
                        $more_exp = Expense::create([
                            'user_id'=>$expense->user_id,
                            "out_trade_no"=>date('ymd', time()) . mt_rand(1000, 9999) . substr(time(), '3', '8'),
                            "device_id"=>$expense['id'],
                            "engine_id"=>$expense['engine_id'],
                            "type"=>'balance',
                            "total"=>$more,
                            "prepaied"=>'0'
                        ]);
                        Trade::create([
                            'user_id'=> $more_exp->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $more_exp->id,
                            'type'=>0,
                            'balance'=> $more,
                            'remark'=>'洗车消费余额'.$more.'元',
                        ]);
                    }else{
                        $more = $total;
                        Trade::create([
                            'user_id'=> $expense->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $expense->id,
                            'type'=>0,
                            'balance'=> $more,
                            'remark'=>'洗车消费余额'.$more.'元',
                        ]);
                    }
                    
                }
            }else{
                if($user['balance_zs'] >=$total){
                    $settlement = 10;
                    $expense->total = 10;
                    $expense->type = 'gift';
                    $expense->finished = 1;
                    $expense->prepaied =0;
                    $expense->save();
                    Trade::create([
                        'user_id'=> $expense->user_id,
                        'trade_type'=>'expense',
                        'order_id'=> $expense->id,
                        'type'=>1,
                        'balance'=> $settlement,
                        'remark'=>'洗车消费赠送余额'.$settlement.'元',
                    ]);
                }else{
                    if($user['balance_zs'] > 0){
                        if($user['balance_zs']>4){
                            $settlement =10;
                        }else{
                            $settlement = 6+$user['balance_zs'];
                        }
                        $expense->total = $user['balance_zs'];
                        $expense->type = 'gift';
                        $expense->finished = 1;
                        $expense->prepaied =0;
                        $expense->save();
                        Trade::create([
                            'user_id'=> $expense->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $expense->id,
                            'type'=>1,
                            'balance'=> $user['balance_zs'],
                            'remark'=>'洗车消费赠送余额'.$user['balance_zs'].'元',
                        ]);
                        $more = $settlement-$user['balance_zs'];
                        $more_exp = Expense::create([
                            'user_id'=>$expense->user_id,
                            "out_trade_no"=>date('ymd', time()) . mt_rand(1000, 9999) . substr(time(), '3', '8'),
                            "device_id"=>$expense['id'],
                            "engine_id"=>$expense['engine_id'],
                            "type"=>'balance',
                            "total"=>$more,
                            "prepaied"=>'0'
                        ]);
                        Trade::create([
                            'user_id'=> $more_exp->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $more_exp->id,
                            'type'=>0,
                            'balance'=> $more,
                            'remark'=>'洗车消费余额'.$more.'元',
                        ]);
                    }else{
                        $settlement = $total>6?$total:6;
                        $expense->total = $settlement;
                        $expense->type = 'balance';
                        $expense->finished = 1;
                        $expense->prepaied =0;
                        $expense->save();
                        Trade::create([
                            'user_id'=> $expense->user_id,
                            'trade_type'=>'expense',
                            'order_id'=> $expense->id,
                            'type'=>0,
                            'balance'=> $settlement,
                            'remark'=>'洗车消费余额'.$settlement.'元',
                        ]);
                    }
                }
            }
            
        }
        return $settlement;
        
    } 
}